package com.atguigu.gmall.common.config.minio.config;

import com.atguigu.gmall.common.config.minio.properties.MinioProperties;
import io.minio.MinioClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfiguration {

    //Springboot给容器放一个组件。 所有参数就是默认从容器中获取
    @Bean
    public MinioClient  minioClient(MinioProperties minioProperties) throws Exception {
        //创建MinioClient
        MinioClient client = new MinioClient(minioProperties.getEndpoint(),minioProperties.getAccessKey(),minioProperties.getSecretKey());

        //判断是否有指定的bucket
        boolean exists = client.bucketExists(minioProperties.getBucketName());
        if(!exists) {
            client.makeBucket(minioProperties.getBucketName());
        }
        return client;
    }
}
